package com.master.demo.example.controller;

import com.master.common.api.QueryDTO;
import com.master.common.api.ResponseInfo;
import com.master.demo.example.command.ValidateCommand;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 接受不同请求方式
 * Post与Put同理
 *
 * @author: Yang
 * @date: 2022/1/7
 * @version: 1.0.0
 * Copyright Ⓒ 2021 恒翔 Computer Corporation Limited All rights reserved.
 */
@RequestMapping("/request")
public class PostController {

    /**
     * RequestBody json对象接收 校验数据
     * content-type:（application/json）
     *
     * @author: Yong
     * @date: 2021/1/7
     * @version: 1.0.0
     */
    @PostMapping(value = "/body")
    public ResponseInfo saveUserByBody(@Validated @RequestBody ValidateCommand command) {
        return ResponseInfo.success(command);
    }

    /**
     * 参数接收
     * content-type:（application/x-www-form-urlencoded）
     *
     * @author: Yang
     * @date: 2021/1/7
     * @version: 1.0.0
     */
    @PostMapping(value = "/form-data1")
    public ResponseInfo saveUserByParam(String username, String password) {
        Map<String, Object> result = new HashMap<>(16);
        result.put("username", username);
        result.put("password", password);
        return ResponseInfo.success(result);
    }

    /**
     * 参数接收
     * content-type:（application/x-www-form-urlencoded）
     *
     * @author: Yang
     * @date: 2021/1/7
     * @version: 1.0.0
     */
    @PostMapping(value = "/form-data2")
    public ResponseInfo saveUserByParam(QueryDTO query) {
        return ResponseInfo.success(query);
    }

    /**
     * 获取请求头和cookie
     *
     * @author: Yang
     * @date: 2021/1/7
     * @version: 1.0.0
     */
    @PostMapping("/")
    public void saveUser(@RequestHeader(name = "token") String token,
                         @CookieValue(name = "cookie") String cookie) {
        System.out.println("token=" + token);
        System.out.println("cookie=" + cookie);
    }

}
